<template >
  <div class="innerPageTable-box-OneTwo value-set-list-wrap innerPageTable-out-flex">
    <!-- 搜索区域 -->
    <SelfAdaption>
      <Form
        :model="searchForm"
        ref="serviceForm"
        slot="adaption-bar"
        label-position="top"
        class="ivu-form-no-margin-bottom"
        inline
      >
        <Form-item prop="vsCode" :label="'规则组名称：'">
          <Input type="text" :maxlength="20" v-model.trim="searchForm.ruleGroupName" :placeholder="'请输入规则组名称'"></Input>
        </Form-item>
        <Form-item prop="reportName" :label="'领域：'">
          <Select filterable v-model="searchForm.reportName">
            <Option v-for="item in areaCodeList" :value="item.value" :key="item.key">{{
              item.value
            }}</Option>
          </Select>
        </Form-item>
        <Form-item prop="ruleGroupStatusCode" :label="'状态：'">
          <Select
            style="width: 100%"
            clearable
            placeholder="请选择状态"
            v-model="searchForm.ruleGroupStatusCode"
          >
            <Option v-for="item in ruleGroupStatusCodeList" :value="item.value" :key="item.value">{{ item.text }}</Option>
          </Select>
        </Form-item>
        <!--  查询 -->
        <Form-item prop="button" class="search-solt-btn">
          <RPButton optType="search" ref='search' @click="searchRuleConfig" />
        </Form-item>
      </Form>
    </SelfAdaption>
    <InnerPageTable ref="RuleConfigTable" :config="pageTableConfig">
      <div slot="table-bar">
        <RPButton optType="create" type="info" @click="createConfig" />
      </div>
    </InnerPageTable>
  </div>
</template>

<script>
import { ruleConfigColumns2 } from './columns'
import ruleConfigApi from '@/api/ruleConfig.js'
import createConfig from './ruleConfigComponents/createConfig2.vue'
import sideDialog from '@/components/sideDialog'
import coverApi from '@/api/cover.js'
import todoApi from '@/api/todoCender.js'
import {
  RULE_CONFIG_STATE_LIST,
  RULE_GROUP_STATUS_LIST,
  RULE_GROUP_STATUS_UN_SUBMIT,
  RULE_GROUP_STATUS_SUBMITED,
  RULE_GROUP_STATUS_EXPIRED,
} from './const.js'
export default {
  name: 'processed',
  data() {
    return {
      areaCodeList: [],
      bussinessUnit: [],
      searchForm: {
        ruleGroupName: '',
        reportName: '',
        ruleGroupStatusCode: ''
      },
      pageTableConfig: {
        loadDataFn: this.getPageInfoList.bind(this),
        columns: ruleConfigColumns2.bind(this)(),
        autoFirst: true
      },
      typeOptions: [
        //  {text: '专项工作', value: '专项工作'},
      ],
      nameOptions: [
        // {text: '关键业务环节数字化覆盖度', value: '关键业务环节数字化覆盖度'},
        // {text: '数字化转型专项实施方案任务表', value: '数字化转型专项实施方案任务表'}
      ],
      allNameOptions: [],
      stateList: RULE_CONFIG_STATE_LIST,
      ruleGroupStatusCodeList: RULE_GROUP_STATUS_LIST,
      menuNo: 'ruleConfig',
      iframeLoginUrl: '',
      iframeSrc: ''
    }
  },
  created() {
    coverApi.getCodeManage(
      {
        commonPageDTO: { currentPage: 1, pageSize: 10 },
        condition: { tenantCode: 'IT_SHARE_DEVELOP', vsCode: 'REPORT_FORM_TYPE' },
        pageNum: 1,
        pageSize: 10,
        sortDTO: { fieldName: '', orderBy: '' },
        tenantCode: 'IT_SHARE_DEVELOP',
        vsCode: 'REPORT_FORM_TYPE'
      }
    ).then(res => {
      this.typeOptions = res.data.records
    })
    coverApi.getCodeManage(
      {
        commonPageDTO: { currentPage: 1, pageSize: 10 },
        condition: { tenantCode: 'IT_SHARE_DEVELOP', vsCode: 'REPORT_FORMS' },
        pageNum: 1,
        pageSize: 10,
        sortDTO: { fieldName: '', orderBy: '' },
        tenantCode: 'IT_SHARE_DEVELOP',
        vsCode: 'REPORT_FORMS'
      }
    ).then(res => {
      this.allNameOptions = res.data.records
    })
  },
  mounted() {
    this.$nextTick(() => {
      this.init()
    })
  },
  watch: {},
  methods: {
    init() {
      // this.searchRuleConfig()
      this.getIframeLoginUrl()
      this.getAreaCodeList()
    },
    getAreaCodeList() {
      todoApi.getAreaCodeList().then(res => {
        if (res.data.code == 200) {
          this.areaCodeList = res.data.data || []
        }
      })
    },
    getIframeLoginUrl() {
      coverApi.getCodeList('IFRAME_OUT_URL').then((res) => {
        let rescords = res.data.records
        if (rescords.length > 0) {
          let list = rescords.filter(item => 'SIGNON_LOGIN_URL' == item.vsiKey) || []
          if (list.length == 1) {
            this.iframeLoginUrl = list[0].vsiDescription
          }
        }
      })
    },
    // 保留勿删，低代码引入开发模式
    createConfig2() {
      coverApi.getCodeList('IFRAME_OUT_URL').then((res) => {
        let rescords = res.data.records
        if (rescords.length > 0) {
          let list = rescords.filter(item => 'RULE_CONFIG_CREATE_URL' == item.vsiKey) || []
          if (list.length == 1) {
            this.iframeSrc = list[0].vsiDescription
            new sideDialog(
              this.iframeSrc,
              {
                fromApp: 'signon',
                iframeLoginUrl: this.iframeLoginUrl
              },
              data => {
                data && this.searchRuleConfig()
              }
            )
          }
        }
      })
    },
    createConfig() {
      new this.$sidelipPage(
        createConfig,
        {
          props: {
            title: '新建规则组',
            action: 'create',
            disabledPage: false,
          }
        },
        data => {
          data && this.searchRuleConfig()
        }
      )
    },
    _submitRuleGroup(ruleGroupId) {
      ruleConfigApi.submitRuleGroup({
        ruleGroupId
      }).then(() => {
        this.$Message.success({
          content: '提交成功',
          duration: 3
        })
        this.searchRuleConfig()
      })
    },
    async goSubmit(row) {
      let res = await ruleConfigApi.getDetail2({id: row.id})
      let detail = {}
      if (res.data.code == 200) {
        detail = res.data.data
      } else {
        this.$Message.error({
          content: '查询规则组详情失败',
          duration: 3
        })
        return
      }
      // 校验规则组
      let ruleList = detail.remindRuleVoList.map((item, index) => {
        return {
          ruleName: '规则' + (index + 1),
          triggerTime: item.triggerTime,
        }
      })
      res = await ruleConfigApi.checkRuleGroup(
        ruleList
      )
      if (res.data.code == 200 && res.data.data) {
        let msg = res.data.data
        msg = msg.replace(/保存/g, '提交')

        this.$confirm({
          title: '',
          content: msg
        }).then(()=>{
          this._submitRuleGroup(row.id)
        })
      } else {
        this._submitRuleGroup(row.id)
      }
    },
    goDetail(row) {
      new this.$sidelipPage(
        createConfig,
        {
          props: {
            title: '提醒规则详情',
            id: row.id,
            action: 'detail',
            disabledPage: true,
          }
        },
        data => {
          data && this.searchRuleConfig()
        }
      )
    },
    // 保留勿删，低代码引入开发模式
    goDetail2(row) {
      coverApi.getCodeList('IFRAME_OUT_URL').then((res) => {
        let rescords = res.data.records
        if (rescords.length > 0) {
          let list = rescords.filter(item => 'RULE_CONFIG_DETAIL_URL' == item.vsiKey) || []
          if (list.length == 1) {
            this.iframeSrc = list[0].vsiDescription
            new sideDialog(
              this.iframeSrc,
              {
                fromApp: 'signon',
                iframeLoginUrl: this.iframeLoginUrl
              },
              data => {
                data && this.searchRuleConfig()
              }
            )
          }
        }
      })
    },
    goEdit(row) {
      new this.$sidelipPage(
        createConfig,
        {
          props: {
            disabledPage: false,
            id: row.id,
            action: 'edit',
            title: '编辑提醒规则'
          }
        },
        data => {
          data && this.searchRuleConfig()
        }
      )
    },
    // 保留勿删，低代码引入开发模式
    goEdit2(row) {
      coverApi.getCodeList('IFRAME_OUT_URL').then((res) => {
        let rescords = res.data.records
        if (rescords.length > 0) {
          let list = rescords.filter(item => 'RULE_CONFIG_EDIT_URL' == item.vsiKey) || []
          if (list.length == 1) {
            this.iframeSrc = list[0].vsiDescription
            new sideDialog(
              this.iframeSrc,
              {
                fromApp: 'signon',
                iframeLoginUrl: this.iframeLoginUrl
              },
              data => {
                data && this.searchRuleConfig()
              }
            )
          }
        }
      })
    },
    goDelete(row) {
      let msg = ''
      if (row.ruleGroupStatusCode == RULE_GROUP_STATUS_UN_SUBMIT) {
        msg = '删除后不可恢复，您确定要删除吗？'
      } else if (row.ruleGroupStatusCode == RULE_GROUP_STATUS_SUBMITED) {
        msg = '该规则组下有尚未发送的提醒邮件，您确认要删除吗？'
      } else if (row.ruleGroupStatusCode == RULE_GROUP_STATUS_EXPIRED) {
        msg = '该规则组已执行完毕，删除后列表将无记录，您确认要删除吗？'
      }
      this.$confirm({
        title:"规则删除",
        content: msg
      }).then(()=>{
        ruleConfigApi.deleteInfo2({
          ruleGroupId: row.id
        }).then(() => {
          this.$Message.success({
            content: '删除成功',
            duration: 3
          })
          this.searchRuleConfig()
        })
      })
    },
    getPageInfoList: tableObj => {
      let { responseProcess, param } = tableObj
      ruleConfigApi.getTableList2(param).then(res => {
       let { pageNum, pageSize, records, pages, total } = res.data.data
        responseProcess({
          pageNum,
          pageSize,
          pages,
          records: records || [],
          total,
          sortOrderBy: "DESC",
          sortPropName: "update_time"
        });
      })
    },
    //搜索
    searchRuleConfig() {
      this.$refs['RuleConfigTable'].load(this.searchForm)
    },
  },
  directives: {}
}
</script>
